<template>
  <div class="chartarea">
    <div class="total">
      <div class="total-title">
        <span>总数</span>
        <span>{{ chartData.total }}</span>
      </div>
    </div>
    <div id="energyPiechart" class="echart"></div>
  </div>
</template>

<script>
import echarts from "@/utils/lib/echarts";
import { PieChart } from "echarts/charts";
echarts.use([PieChart]);
export default {
  name: "energyPiechart",
  props: {
    // 图数据
    chartData: {
      type: Object,
      default: {},
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  mounted() {
    this.getChartsdata(this.chartData);
  },
  beforeDestroy() {
    this.chartDes();
  },
  watch: {
    chartData: {
      handler(val) {
        if (val) {
          this.chartDes();
          this.getChartsdata(this.chartData);
        }
      },
      deep: false,
      immediate: false,
    },
  },
  methods: {
    chartDes() {
      if (!this.chart) {
        return;
      }
      this.chart.dispose();
      this.chart = null;
    },
    getChartsdata(chartData) {
      let option = {
        // title: {
        //   text: '',
        //   subtext: '',
        //   left: 'center'
        // },
        tooltip: {
          trigger: "item",
          valueFormatter: function (value) {
            return value + " 条";
          },
        },
        legend: {
          orient: "vertical",
          top: "20%",
          padding: 0,
          right: "0%",
          icon: "circle",
          itemGap: 3,
          textStyle: {
            color: "#8C8C8C",
            fontFamily: "PingFang-SC-Regular, PingFang-SC",
            fontWeight: 400,
            lineHeight: 17,
          },
        },
        series: [
          {
            name: "",
            type: "pie",
            radius: "50%",
            center: ["40%", "50%"],
            minAngle: 5, //设置扇形的最小占比
            itemStyle: {
              borderRadius: 10,
              // borderColor: '#fff',
              // borderWidth: 2
            },
            label: {
              show: true,
              formatter: function (params) {
                let a = params.name;
                let b = params.value;
                return a + b;
              },
            },
            data: chartData.seriesData,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)",
              },
            },
          },
        ],
      };
      this.initChart("energyPiechart", option);
    },
    initChart(id, options) {
      this.chart = echarts.init(document.getElementById(id));
      this.chart.setOption(options);
      window.addEventListener("resize", () => {
        if (this.chart) {
          this.chart.resize();
        }
      });
    },
  },
};
</script>

<style scoped lang="scss">
.chartarea {
  width: 100%;
  .total {
    width: 100%;
    height: 40px;
    .total-title {
      // position: relative;
      // left: 30%;
      width: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
      span {
        font-family: SourceHanSansSC;
        text-decoration: none;
        font-style: normal;
        letter-spacing: 0px;
        &:nth-child(1) {
          font-weight: 400;
          font-size: 16px;
          color: rgb(16, 16, 16);
          line-height: 24px;
        }
        &:nth-child(2) {
          font-weight: 700;
          font-size: 28px;
          color: rgb(64, 149, 229);
          line-height: 40px;
        }
      }
    }
  }
  .echart {
    width: 100%;
    height: 240px;
  }
}
</style>
